package com.generator.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.generator.demo.bg.entity.BgTest;
import com.generator.demo.bg.mapper.BgTestMapper;
import com.generator.demo.common.pageHelp.PageVo;
import com.generator.demo.model.dto.*;
import com.generator.demo.model.vo.TestVo;
import com.generator.demo.service.TestService;
import com.generator.demo.utils.BeanUtil;
import com.generator.demo.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 *
 * @author fxh
 * @since 2024-07-31
 */
@Service
public class TestServiceImpl extends ServiceImpl<BgTestMapper, BgTest>  implements TestService {

//    @Resource
//    private BgTestService bgTestService;

    @Resource
    private BgTestMapper bgTestMapper;

    @Override
    public void add(TestAddDto testAddDto){
        //dto转换bg
        BgTest bgTest = BeanUtil.toModelClass(testAddDto,BgTest.class);
        //保存
        this.save(bgTest);
    }

    @Override
    public void remove(TestRemoveDto testRemoveDto){

        //根据id删除
        this.removeById("自己处理");
    }

    @Override
    public void change(TestChangeDto testChangeDto){
        //dto转换bg
        BgTest bgTest = BeanUtil.toModelClass(testChangeDto,BgTest.class);
        //修改
        this.updateById(bgTest);
    }

    @Override
    public List<TestVo> list(TestListDto testListDto){
        //dto转换bg
        BgTest bgTest = BeanUtil.toModelClass(testListDto,BgTest.class);
        LambdaQueryWrapper<BgTest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(bgTest);
        List<BgTest> bgTestList = this.list(queryWrapper);
        //转换vo
        List<TestVo> testVoList = BeanUtil.toModelList(bgTestList,TestVo.class);
        return testVoList;
    }

    @Override
    public PageVo<TestVo> page(TestPageDto test){
        //获取iPage
        IPage<BgTest> iPage = TestPageDto.withPage(test);
        //dto转换bg
        BgTest bgTest = BeanUtil.toModelClass(test,BgTest.class);
        LambdaQueryWrapper<BgTest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(bgTest);
        //查询page
        IPage<BgTest> bgTestPage = this.page(iPage,queryWrapper);
        //提取tbList
        List<BgTest> bgTestList = bgTestPage.getRecords();
        PageVo<TestVo> testVoPage = null;
        if(EmptyUtil.isNotEmptyOfCollection(bgTestList)){
            //bgList转voList
            List<TestVo> testVoList = BeanUtil.toModelList(bgTestList,TestVo.class);
            //转pageVo
            PageVo<BgTest> bgTestPageVo = PageVo.withIPage(bgTestPage);
            testVoPage = PageVo.transform(bgTestPageVo, testVoList);
        }
        return testVoPage;
    }
}